#include "RemoconWidget.h"

Button::Button(const QString &s, unsigned char key, QWidget *parent): QPushButton(s, parent, NULL),
		_keyCode(key)
{
	connect(this, SIGNAL(pressed()), this, SLOT(emitPressed()));
	connect(this, SIGNAL(released()), this, SLOT(emitReleased()));
	connect(this, SIGNAL(clicked()), this, SLOT(emitClicked()));
}

void Button::emitPressed()
{
	emit pressed(_keyCode);
}

void Button::emitReleased()
{
	emit released(_keyCode);
}

void Button::emitClicked()
{
	emit clicked(_keyCode);
}

#include <qpushbutton.h>
#include <qlayout.h>

#include "RemoconWidget.h"
#include "winuser.h"

RemoconWidget::RemoconWidget(QWidget *parent): QWidget(parent, NULL)
{
	_mediaCenter = new QPushButton(tr("WMC"), this);
	connect(_mediaCenter, SIGNAL(clicked()), this, SLOT(launchMediaCenter()));
	
	_music = new QPushButton(tr("Music"), this);
	connect(_music, SIGNAL(clicked()), this, SLOT(openMusic()));
	
	_tv = new QPushButton(tr("TV"), this);
	connect(_music, SIGNAL(clicked()), this, SLOT(openTV()));
	
	_radio = new QPushButton(tr("Radio"), this);
	connect(_music, SIGNAL(clicked()), this, SLOT(openRadio()));
	
	_image = new QPushButton(tr("Image"), this);
	connect(_music, SIGNAL(clicked()), this, SLOT(openImage()));
	
	_video = new QPushButton(tr("Video"), this);
	connect(_music, SIGNAL(clicked()), this, SLOT(openVideo()));
	
	_dvd = new QPushButton(tr("DVD"), this);
	connect(_music, SIGNAL(clicked()), this, SLOT(openDVD()));
	
	_menu = new Button(tr("Menu"), VK_APPS, this);
	
	_play = new Button(tr("Play/Pause"), VK_MEDIA_PLAY_PAUSE, this);
	_stop = new Button(tr("Stop"), VK_MEDIA_STOP, this);
	_prev = new Button(tr("Prev"), VK_MEDIA_PREV_TRACK, this);
	_next = new Button(tr("Next"), VK_MEDIA_NEXT_TRACK, this);
	
	_mute = new Button(tr("Mute"), VK_VOLUME_MUTE, this);
	_volUp = new Button(tr("+"), VK_VOLUME_UP, this);
	_volDown = new Button(tr("-"), VK_VOLUME_DOWN, this);
	
	_up = new Button(tr("Up"), VK_UP, this);
	_down = new Button(tr("Down"), VK_DOWN, this);
	_left = new Button(tr("Left"), VK_LEFT, this);
	_right = new Button(tr("Right"), VK_RIGHT, this);
	
	_enter = new Button(tr("Enter"), VK_RETURN, this);
	_back = new Button(tr("Back"), VK_BROWSER_BACK, this);
	
	this->connect(_menu, SIGNAL(pressed(unsigned char)), this, SIGNAL(keyPressed(unsigned char)));
	this->connect(_play, SIGNAL(pressed(unsigned char)), this, SIGNAL(keyPressed(unsigned char)));
	this->connect(_stop, SIGNAL(pressed(unsigned char)), this, SIGNAL(keyPressed(unsigned char)));
	this->connect(_prev, SIGNAL(pressed(unsigned char)), this, SIGNAL(keyPressed(unsigned char)));
	this->connect(_next, SIGNAL(pressed(unsigned char)), this, SIGNAL(keyPressed(unsigned char)));
	this->connect(_volUp, SIGNAL(pressed(unsigned char)), this, SIGNAL(keyPressed(unsigned char)));
	this->connect(_volDown, SIGNAL(pressed(unsigned char)), this, SIGNAL(keyPressed(unsigned char)));
	this->connect(_mute, SIGNAL(pressed(unsigned char)), this, SIGNAL(keyPressed(unsigned char)));
	this->connect(_up, SIGNAL(pressed(unsigned char)), this, SIGNAL(keyPressed(unsigned char)));
	this->connect(_down, SIGNAL(pressed(unsigned char)), this, SIGNAL(keyPressed(unsigned char)));
	this->connect(_left, SIGNAL(pressed(unsigned char)), this, SIGNAL(keyPressed(unsigned char)));
	this->connect(_right, SIGNAL(pressed(unsigned char)), this, SIGNAL(keyPressed(unsigned char)));
	this->connect(_enter, SIGNAL(pressed(unsigned char)), this, SIGNAL(keyPressed(unsigned char)));
	this->connect(_back, SIGNAL(pressed(unsigned char)), this, SIGNAL(keyPressed(unsigned char)));
	
	this->connect(_menu, SIGNAL(released(unsigned char)), this, SIGNAL(keyReleased(unsigned char)));
	this->connect(_play, SIGNAL(released(unsigned char)), this, SIGNAL(keyReleased(unsigned char)));
	this->connect(_stop, SIGNAL(released(unsigned char)), this, SIGNAL(keyReleased(unsigned char)));
	this->connect(_prev, SIGNAL(released(unsigned char)), this, SIGNAL(keyReleased(unsigned char)));
	this->connect(_next, SIGNAL(released(unsigned char)), this, SIGNAL(keyReleased(unsigned char)));
	this->connect(_volUp, SIGNAL(released(unsigned char)), this, SIGNAL(keyReleased(unsigned char)));
	this->connect(_volDown, SIGNAL(released(unsigned char)), this, SIGNAL(keyReleased(unsigned char)));
	this->connect(_mute, SIGNAL(released(unsigned char)), this, SIGNAL(keyReleased(unsigned char)));
	this->connect(_up, SIGNAL(released(unsigned char)), this, SIGNAL(keyReleased(unsigned char)));
	this->connect(_down, SIGNAL(released(unsigned char)), this, SIGNAL(keyReleased(unsigned char)));
	this->connect(_left, SIGNAL(released(unsigned char)), this, SIGNAL(keyReleased(unsigned char)));
	this->connect(_right, SIGNAL(released(unsigned char)), this, SIGNAL(keyReleased(unsigned char)));
	this->connect(_enter, SIGNAL(released(unsigned char)), this, SIGNAL(keyReleased(unsigned char)));
	this->connect(_back, SIGNAL(released(unsigned char)), this, SIGNAL(keyReleased(unsigned char)));
	
	QHBoxLayout *jl = new QHBoxLayout();
	jl->addWidget(_music);
	jl->addWidget(_tv);
	jl->addWidget(_radio);
	jl->addWidget(_image);
	jl->addWidget(_video);
	jl->addWidget(_dvd);
	
	QGridLayout *g = new QGridLayout(this, 8, 3);
	g->addMultiCell(jl, 0, 0, 0, 2);
	g->addWidget(_prev, 1, 0);
	g->addWidget(_play, 1, 1);
	g->addWidget(_next, 1, 2);
	g->addWidget(_menu, 2, 0);
	g->addWidget(_stop, 2, 1);
	g->addWidget(_volUp, 2, 2);
	g->addWidget(_back, 3, 0);
	g->addWidget(_volDown, 3, 2);
	g->addWidget(_mediaCenter, 4, 1);
	g->addWidget(_up, 5, 1);
	g->addWidget(_mute,5, 2);
	g->addWidget(_left, 6, 0);
	g->addWidget(_right, 6, 2);
	g->addWidget(_enter, 6, 1);
	g->addWidget(_down, 7, 1);
}

/*
	// Win + Alt + Enter
	// Ctrl + M
	// Ctrl + T
	// Ctrl + A
	// Ctrl + I
	// Ctrl + E
	// Ctrl + Shift + M
*/
void RemoconWidget::launchMediaCenter()
{
	emit keyPressed(VK_LWIN);
	emit keyPressed(VK_MENU);
	emit keyPressed(VK_RETURN);
	emit keyReleased(VK_RETURN);
	emit keyReleased(VK_MENU);
	emit keyReleased(VK_LWIN);
}

void RemoconWidget::openMusic()
{
	emit keyPressed(VK_CONTROL);
	emit keyPressed('M');
	emit keyReleased('M');
	emit keyReleased(VK_CONTROL);
}

void RemoconWidget::openTV()
{
	emit keyPressed(VK_CONTROL);
	emit keyPressed('T');
	emit keyReleased('T');
	emit keyReleased(VK_CONTROL);
}

void RemoconWidget::openRadio()
{
	emit keyPressed(VK_CONTROL);
	emit keyPressed('A');
	emit keyReleased('A');
	emit keyReleased(VK_CONTROL);
}

void RemoconWidget::openImage()
{
	emit keyPressed(VK_CONTROL);
	emit keyPressed('I');
	emit keyReleased('I');
	emit keyReleased(VK_CONTROL);
}

void RemoconWidget::openVideo()
{
	emit keyPressed(VK_CONTROL);
	emit keyPressed('E');
	emit keyReleased('E');
	emit keyReleased(VK_CONTROL);
}

void RemoconWidget::openDVD()
{
	emit keyPressed(VK_CONTROL);
	emit keyPressed(VK_SHIFT);
	emit keyPressed('M');
	emit keyReleased('M');
	emit keyReleased(VK_SHIFT);
	emit keyReleased(VK_CONTROL);
}
